rust: macros: add if_cfg! macro for config-based conditionals#1218
Open
KaiserGranatapfel wants to merge 1 commit intoRust-for-Linux:rust-nextfrom
Open
rust: macros: add if_cfg! macro for config-based conditionals#1218KaiserGranatapfel wants to merge 1 commit intoRust-for-Linux:rust-nextfrom
KaiserGranatapfel wants to merge 1 commit intoRust-for-Linux:rust-nextfrom
Conversation
Author
KaiserGranatapfel
left a comment
There was a problem hiding this comment.
Self-review
- Code compiles and passes formatting/linting checks
- Macro implementation follows existing kernel macro patterns
- Documentation complete with examples
- Tested with existing conditional compilation patterns in
time.rs
Ready for review.
This macro simplifies conditional compilation patterns by providing a
more readable syntax for selecting code based on kernel configuration
options. It replaces verbose #[cfg(...)] and #[cfg(not(...))] blocks
with a cleaner if_cfg!(if CONFIG_FOO { expr1 } else { expr2 }) syntax.
The macro works in both expression and statement positions, making it
versatile for various use cases throughout the kernel.
Examples of usage:
- Expression: let value = if_cfg!(if CONFIG_64BIT { x / y } else { unsafe { div64_s64(x, y) } });
- Statement: if_cfg!(if CONFIG_FOO { let x = 1; } else { let x = 2; });
This implementation also includes updates to rust/kernel/time.rs to
demonstrate the macro's usage, replacing several instances of the
verbose conditional compilation pattern.
Suggested-by: Rust-for-Linux Issue Rust-for-Linux#1183 <Rust-for-Linux#1183>
Link: Rust-for-Linux#1183
Signed-off-by: KaiserGranatapfel <christopher.erleigh@gmail.com>
7dabd09 to
f0458e2
Compare
Member
|
Note that we don't use PRs anymore (you can leave it open if you think it is useful for some reason, though). I see you sent the patch to the mailing list but it didn't arrive there: https://lore.kernel.org/rust-for-linux/CANiq72nMpO0o-P0BbTgxPU4RSOttFPc4UQzH9Bs73F7Bnkyxmg@mail.gmail.com/ Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR implements the
if_cfg!macro as requested in issue #1183. The macro simplifies conditional compilation patterns by providing a more readable syntax for selecting code based on kernel configuration options.Changes
if_cfg!macro torust/kernel/lib.rsrust/kernel/time.rsto demonstrate usage (4 instances replaced)Benefits
#[cfg(...)]/#[cfg(not(...))]blocksTesting
rustfmtRelated
Closes #1183